package com.liu.stockmarket.ui.search

import android.content.res.ColorStateList
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.liu.stockmarket.R
import com.liu.stockmarket.model.Stock
import java.io.Serializable

class SearchResultAdapter :
    BaseQuickAdapter<SearchResultAdapter.Item, BaseViewHolder>(R.layout.item_search_result) {


    init {
        addChildClickViewIds(R.id.iv_img)
    }



    override fun convert(holder: BaseViewHolder, item: Item) {
        holder.setText(R.id.tv_name, item.name)
            .setText(R.id.tv_code, item.code)
            .setText(R.id.tv_type, item.type)
            .setVisible(R.id.tv_is_add, item.id != null)

        with(holder.getView<TextView>(R.id.tv_is_add)) {
            if (item.transactionId!=null && item.selfId !=null) {
                this.visibility = View.VISIBLE
                this.text = "已添加、加自选"
            }else if (item.transactionId!=null && item.selfId ==null){
                this.visibility = View.VISIBLE
                this.text = "已添加"
            }else if (item.transactionId==null && item.selfId !=null){
                this.visibility = View.VISIBLE
                this.text = "加自选"
            }else{
                this.visibility = View.GONE
            }
        }
        with(holder.getView<ImageView>(R.id.iv_img)){
            if (item.selfId ==null) {
                this.setImageDrawable(  AppCompatResources.getDrawable(context, R.drawable.ic_baseline_add_circle_24))
                this.imageTintList = ColorStateList.valueOf(context.getColor(R.color.color_primary))
            } else {
                this.setImageDrawable( AppCompatResources.getDrawable(context, R.drawable.ic_baseline_check_circle_24))
                this.imageTintList = ColorStateList.valueOf(context.getColor(R.color.gray_double))
            }
        }
    }

    data class Item(
        var id: Long? = null,
        val code: String,
        val name: String,
        val type: String,
        val stockType: Stock.StockType,
        val transactionExchange: Stock.TransactionExchange,
        /**
         * 自选ID
         */
        val selfId:Long?=null,
        /**
         * 已交易
         */
        val transactionId:Long?=null

    ) : Serializable
}